package com.example.myapplication.frament;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.me.Set;
import com.example.myapplication.okhttp.HttpUtil;
import com.example.myapplication.register.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FragmentMe extends Fragment {

    private TextView tv_name;
    private TextView tv_national;
    private ImageView iv_head;
    private ImageView iv_setting;
    private EditText user_name;
    private EditText user_pwd;
    private Button login;
    private Button register;
    private CardView card;
    private LinearLayout layout;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext() , R.layout.fragment_me , null);
        tv_name = view.findViewById(R.id.me_name);
        tv_national = view.findViewById(R.id.me_national);
        iv_head = view.findViewById(R.id.touxiang);
        user_name = view.findViewById(R.id.edit_name);
        user_pwd = view.findViewById(R.id.edit_pwd);
        login = view.findViewById(R.id.btn_login);
        register = view.findViewById(R.id.register);
        card =  view.findViewById(R.id.card);
        layout = view.findViewById(R.id.layout);
        iv_setting = view.findViewById(R.id.me_img_btn);
        checkData();
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = user_name.getText().toString();
                String s1 = user_pwd.getText().toString();
                if (!s.isEmpty()&&!s1.isEmpty()){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject user = HttpUtil.getUser("users?user_name=" + s);
                                String s2 = user.getString("user_national");
                                String s3 = user.getString("user_addr");
                                if (user.getString("user_password").equals(s1)){
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                                            tv_name.setVisibility(View.VISIBLE);
                                            tv_national.setVisibility(View.VISIBLE);
                                            iv_head.setVisibility(View.VISIBLE);
                                            user_name.setVisibility(View.INVISIBLE);
                                            card.setVisibility(View.INVISIBLE);
                                            tv_name.setText(s);
                                            tv_national.setText(s2);
                                            layout.setBackgroundResource(R.drawable.me_bg);
                                            iv_setting.setVisibility(View.VISIBLE);
                                            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user",getActivity().MODE_PRIVATE);
                                            SharedPreferences.Editor edit = sharedPreferences.edit();
                                            edit.putString("user_name" , s);
                                            edit.putString("user_pwd" , s1);
                                            edit.putString("national" , s2);
                                            edit.putString("addr" , s3);
                                            edit.commit();
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
        return view;
    }

    private void register() {
        getActivity().startActivity(new Intent(getContext() , User.class));
        iv_head.setEnabled(false);
    }

    private void checkData() {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user",getActivity().MODE_PRIVATE);
        String name = sharedPreferences.getString("user_name" , "");
        String pwd = sharedPreferences.getString("user_pwd" , "");
        String national = sharedPreferences.getString("national" , "");
        String addr = sharedPreferences.getString("addr" , "");
        if (!name.isEmpty()&&!pwd.isEmpty()&&!national.isEmpty()){
            showData(name , pwd , national , addr);
            iv_head.setEnabled(false);
        }else{
            setNull();
        }
    }

    private void setNull() {
        card.setVisibility(View.VISIBLE);
    }

    private void showData(String name, String pwd , String national , String addr) {
        layout.setBackgroundResource(R.drawable.me_bg);
        iv_head.setVisibility(View.VISIBLE);
        tv_name.setText(name);
        tv_name.setVisibility(View.VISIBLE);
        tv_national.setVisibility(View.VISIBLE);
        tv_national.setText(national);
        iv_setting.setVisibility(View.VISIBLE);
    }

}
